const { contextBridge, ipcRenderer} = require('electron')

/**
 * ipcRenderer.send  ipcMain.on (单向)
 * ipcRenderer.invoke  ipcMain.handle (双向，返回一个promise)
 * */

// contextBridge.exposeInMainWorld('electronAPI', {
//   setTitle: (title) => ipcRenderer.send('set-title', title),
//   postMessage: () => ipcRenderer.invoke('post-message')
// })


// contextBridge.exposeInMainWorld('electronAPI', {
//   toggleWindow: () => ipcRenderer.send('toggle-window'),
//   // 暴露其他需要的方法...
// });

contextBridge.exposeInMainWorld('electronAPI', {
  send: (channel, ...args) => {
    ipcRenderer.send(channel, ...args)
  },
  on: (channel, func) => {
    ipcRenderer.on(channel, func)
  },
  once: (channel, func) => {
    ipcRenderer.once(channel, func)
  },
  removeListener: (channel, func) => {
    ipcRenderer.removeListener(channel, func)
  },
  sendSync: (channel, ...args) => {
    return ipcRenderer.sendSync(channel, ...args)
  },
  invoke: (channel, ...args) => {
    try {
      return ipcRenderer.invoke(channel, ...args)
    } catch (error) {
      console.error(`Error invoking API: ${channel}`, error)
    }
  },
})


